﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ornament.Util.NHibernateHelper;

public partial class Admin_System_Controls_NhibernateControls_BaseInfo : UserControl
{

    private void DataBindDriverType(DataBaseType dataBaseType, string selectedDriverClas)
    {
        bool foundSelected = false;
        DropDownListDriverClass.Items.Clear();
        foreach (string drivers in dataBaseType.SuggestionDriverClass)
        {
            var li = new ListItem {Text = drivers, Value = drivers};
            DropDownListDriverClass.Items.Add(li);
            if (drivers == selectedDriverClas)
            {
                foundSelected = true;
                li.Selected = true;
            }
        }
        if (!foundSelected)
            DropDownListDriverClass.Items[0].Selected = true;
    }


    public void DataBind(SessionFactory sessionFactory)
    {
        DriverInfo dialect = sessionFactory.DriverInfo;
        foreach (ListItem driversType in DatabaseTypeDropDownlist.Items)
        {
            if (driversType.Value == dialect.Dialect)
            {
                driversType.Selected = true;
                break;
            }
        }
        DataBindDriverType(dialect.DataBase, dialect.DriverClass);
        TextBoxConnectionString.Text = sessionFactory.DriverInfo.ConnectionString;
        LabelDialectName.Text = sessionFactory.DriverInfo.Dialect;
    }

    public void UpdateValueOn(SessionFactory sessionFactory)
    {
        sessionFactory.DriverInfo.ConnectionString = TextBoxConnectionString.Text;
        sessionFactory.DriverInfo.DataBase = DataBaseType.SupportDataBaseTypes[DatabaseTypeDropDownlist.SelectedValue];
        sessionFactory.DriverInfo.DriverClass = DropDownListDriverClass.SelectedValue;
    }


    protected void DataTypeInit(object sender, EventArgs e)
    {
        foreach (DataBaseType dt in DataBaseType.SupportDataBaseTypes)
        {
            var li = new ListItem {Text = dt.DataBaseName, Value = dt.Dialect};
            DatabaseTypeDropDownlist.Items.Add(li);
        }
    }

    protected void DataTypeChanged(object sender, EventArgs e)
    {
        DataBaseType d = DataBaseType.SupportDataBaseTypes[DatabaseTypeDropDownlist.SelectedValue];
        DataBindDriverType(d, null);
        LabelDialectName.Text = d.Dialect;
    }
}